package com.xinfu.newproject.ui.complaint_feedback;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.adapter.complaint_adapter.ComplaintDetailAdapter;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.complaint.bean.ComplaintDetailBean;
import com.xinfu.newproject.bean.complaint.bean.ComplaintEvaluateBean;
import com.xinfu.newproject.bean.complaint.bean.ComplaintRecordBean;
import com.xinfu.newproject.bean.complaint.bean.SubmitComplaintBean;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.presenter.ComplaintPresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;
import com.xinfu.newproject.widget.picture.ViewPagerActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;

public class ComplaintDetailsFragment extends BaseActivity implements IView.IComplaintView {

    @BindView(R.id.fragment_complaint_details_rv)
    RecyclerView mRecyclerView;

    @BindView(R.id.fragment_repair_detail_process_tv)
    TextView fragmentProcessTv;

    @BindView(R.id.layout_head_status_rl)
    RelativeLayout headRl;

    private ComplaintDetailAdapter complaintDetailAdapter;
    private ComplaintPresenter complaintPresenter = new ComplaintPresenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_complaint_details);
        ButterKnife.bind(this);
        registerBack();
        setActivityTitle("投诉建议详情");
        initViewBind();
        initLisener();
    }

    int mSuspensionHeight, processTvHeight;
    private void initLisener() {

        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                mSuspensionHeight = headRl.getHeight();
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if (mRecyclerView != null && processTvHeight != 0) {
//                    Log.i("testHeight",mRecyclerView.getChildAt(0).getHeight()+"");
//                    Log.i("testHeight","顶部高度"+mSuspensionHeight+"");
                    if (mRecyclerView.getChildAt(0).getHeight() < processTvHeight) {
                        fragmentProcessTv.setVisibility(View.VISIBLE);
                    } else {
                        fragmentProcessTv.setVisibility(View.GONE);
                    }
                }

            }
        });

    }

    private void initViewBind() {

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        initAdapter();

        View headerView = getHeaderView();
        complaintDetailAdapter.addHeaderView(headerView);
        mRecyclerView.setAdapter(complaintDetailAdapter);
        ComplaintRecordBean.ResultBean resultBean1 = new Gson().fromJson(getIntent().getExtras().getString("response"), ComplaintRecordBean.ResultBean.class);
        if (button != null) {
            if (resultBean1.getStatus() == 200) {
                button.setVisibility(View.VISIBLE);
            } else {
                button.setVisibility(View.GONE);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isLogin()) {
            complaintPresenter.complaintDetail();
        }
    }

    TextView contentTv, titleCommunityTv, processTv;
    ImageView imageView1, imageView2, imageView3, imageView4;
    Button button;

    private View getHeaderView() {
        View view = getLayoutInflater().inflate(R.layout.recyclerview_header_complaint_detail, (ViewGroup) mRecyclerView.getParent(), false);
        LinearLayout linearLayout = view.findViewById(R.id.header_repair_detail_ll);

        contentTv = view.findViewById(R.id.recycler_header_complaint_detail_content);
        titleCommunityTv = view.findViewById(R.id.recycler_header_complaint_detail_title);
        imageView1 = view.findViewById(R.id.recycler_header_complaint_detail_iv1);
        imageView2 = view.findViewById(R.id.recycler_header_complaint_detail_iv2);
        imageView3 = view.findViewById(R.id.recycler_header_complaint_detail_iv3);
        imageView4 = view.findViewById(R.id.recycler_header_complaint_detail_iv4);
        processTv = view.findViewById(R.id.header_repair_detail_process_tv);
        button = view.findViewById(R.id.header_repair_detail_evaluate);

        linearLayout.setVisibility(View.GONE);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("response", getIntent().getExtras().getString("response"));
                startIntentActivity(ComplaintEvaluateActivity.class, bundle);
            }
        });
        return view;
    }


    private void initAdapter() {
        complaintDetailAdapter = new ComplaintDetailAdapter();
//        complaintDetailAdapter.openLoadAnimation();
//        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
//            @Override
//            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(HeaderAndFooterUseActivity.this, "" + Integer.toString(position), Toast.LENGTH_LONG).show();
//            }
//        });
        complaintDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });

    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName, complaintId, tenantId;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(this, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            BToast.showText(this, "未登录");
            return false;
        }

        if (getIntent().getExtras() != null) {
            ComplaintRecordBean.ResultBean resultBean1 = new Gson().fromJson(getIntent().getExtras().getString("response"), ComplaintRecordBean.ResultBean.class);
            if (resultBean1.getType().equals("1")) {
                setActivityTitle("投诉详情");
            } else if (resultBean1.getType().equals("0")) {
                setActivityTitle("建议详情");
            }
            complaintId = resultBean1.getId();
        } else {
            return false;
        }
        return true;
    }

    @Override
    public String header() {
        return token;
    }

    @Override
    public String userId() {
        return null;
    }

    @Override
    public String type() {
        return null;
    }

    @Override
    public String name() {
        return null;
    }

    @Override
    public String telephone() {
        return null;
    }

    @Override
    public String picture() {
        return null;
    }

    @Override
    public String content() {
        return null;
    }

    @Override
    public String projectId() {
        return null;
    }

    @Override
    public String complaintId() {
        return complaintId;
    }

    @Override
    public String tenantId() {
        return null;
    }

    @Override
    public Integer status() {
        return null;
    }

    @Override
    public void showToast(String content) {

    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void getMsg(SubmitComplaintBean submitComplaintBean) {

    }

    @Override
    public void getMsg(ComplaintRecordBean complaintRecordBean) {

    }

    @Override
    public void getMsg(ComplaintDetailBean complaintDetailBean) {
        if (complaintDetailBean.getResult() != null) {
            if (complaintDetailBean.getResult().getComplaintStatusList().size() > 0) {
                if (mRecyclerView != null) {
                    processTvHeight = mRecyclerView.getChildAt(0).getHeight();
                }
            }
            if (button != null) {
                if (complaintDetailBean.getResult().getStatus() == 200) {
                    button.setVisibility(View.VISIBLE);
                } else {
                    button.setVisibility(View.GONE);
                }
            }
            List<ComplaintDetailBean.ResultBean.ComplaintStatusListBean> list = new ArrayList<>();
            titleCommunityTv.setText(complaintDetailBean.getResult().getProjectName());
            contentTv.setText(complaintDetailBean.getResult().getContent());
            if (complaintDetailBean.getResult().getPicture() != null) {
                if (complaintDetailBean.getResult().getPicture().length() > 5) {
                    String[] imageList = complaintDetailBean.getResult().getPicture().split(";");
                    switch (imageList.length) {
                        case 1:
                            imageView1.setVisibility(View.VISIBLE);
                            imageView2.setVisibility(View.GONE);
                            imageView3.setVisibility(View.GONE);
                            imageView4.setVisibility(View.GONE);
                            Glide.with(this).load(imageList[0]).asBitmap().error(R.mipmap.ic_launcher).into(imageView1);
                            break;
                        case 2:
                            imageView1.setVisibility(View.VISIBLE);
                            imageView2.setVisibility(View.VISIBLE);
                            imageView3.setVisibility(View.GONE);
                            imageView4.setVisibility(View.GONE);
                            Glide.with(this).load(imageList[0]).asBitmap().error(R.mipmap.ic_launcher).into(imageView1);
                            Glide.with(this).load(imageList[1]).asBitmap().error(R.mipmap.ic_launcher).into(imageView2);
                            break;
                        case 3:
                            imageView1.setVisibility(View.VISIBLE);
                            imageView2.setVisibility(View.VISIBLE);
                            imageView3.setVisibility(View.VISIBLE);
                            imageView4.setVisibility(View.GONE);
                            Glide.with(this).load(imageList[0]).asBitmap().error(R.mipmap.ic_launcher).into(imageView1);
                            Glide.with(this).load(imageList[1]).asBitmap().error(R.mipmap.ic_launcher).into(imageView2);
                            Glide.with(this).load(imageList[2]).asBitmap().error(R.mipmap.ic_launcher).into(imageView3);
                            break;
                        case 4:
                            imageView1.setVisibility(View.VISIBLE);
                            imageView2.setVisibility(View.VISIBLE);
                            imageView3.setVisibility(View.VISIBLE);
                            imageView4.setVisibility(View.VISIBLE);
                            Glide.with(this).load(imageList[0]).asBitmap().error(R.mipmap.ic_launcher).into(imageView1);
                            Glide.with(this).load(imageList[1]).asBitmap().error(R.mipmap.ic_launcher).into(imageView2);
                            Glide.with(this).load(imageList[2]).asBitmap().error(R.mipmap.ic_launcher).into(imageView3);
                            Glide.with(this).load(imageList[3]).asBitmap().error(R.mipmap.ic_launcher).into(imageView4);
                            break;
                    }
                } else {
                    imageView1.setVisibility(View.GONE);
                    imageView2.setVisibility(View.GONE);
                    imageView3.setVisibility(View.GONE);
                    imageView4.setVisibility(View.GONE);
                }
            } else {
                imageView1.setVisibility(View.GONE);
                imageView2.setVisibility(View.GONE);
                imageView3.setVisibility(View.GONE);
                imageView4.setVisibility(View.GONE);
            }
            imageView1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle1 = new Bundle();
                    bundle1.putString("picture", complaintDetailBean.getResult().getPicture());
                    bundle1.putInt("position", 0);
                    startIntentActivityAnim(ViewPagerActivity.class, imageView1, bundle1);
                }
            });
            imageView2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle1 = new Bundle();
                    bundle1.putString("picture", complaintDetailBean.getResult().getPicture());
                    bundle1.putInt("position", 1);
                    startIntentActivityAnim(ViewPagerActivity.class, imageView2, bundle1);
                }
            });
            imageView3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle1 = new Bundle();
                    bundle1.putString("picture", complaintDetailBean.getResult().getPicture());
                    bundle1.putInt("position", 2);
                    startIntentActivityAnim(ViewPagerActivity.class, imageView3, bundle1);
                }
            });
            imageView4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle1 = new Bundle();
                    bundle1.putString("picture", complaintDetailBean.getResult().getPicture());
                    bundle1.putInt("position", 3);
                    startIntentActivityAnim(ViewPagerActivity.class, imageView4, bundle1);
                }
            });
            if (complaintDetailBean.getResult().getComplaintStatusList().size() > 0) {
                for (int i = 0; i < complaintDetailBean.getResult().getComplaintStatusList().size(); i++) {
                    if (i == 0) {
                        complaintDetailBean.getResult().getComplaintStatusList().get(0).setPosition(0);
                    } else {
                        complaintDetailBean.getResult().getComplaintStatusList().get(i).setPosition(1);
                    }
                    list.add(complaintDetailBean.getResult().getComplaintStatusList().get(i));
                }
            }
            complaintDetailAdapter.detail(complaintDetailBean.getResult().getName(), complaintDetailBean.getResult().getCreateTime(), complaintDetailBean.getResult().getType());
            complaintDetailAdapter.setNewData(list);
        }

    }

    @Override
    public void getMsg(ComplaintEvaluateBean complaintEvaluateBean) {

    }
}